import os
import platform
import socket
import subprocess
import psutil
from pyroute2 import IPRoute

def get_system_info():
    """获取系统信息"""
    system_info = {
        "Platform": platform.platform(),
        "Hostname": socket.gethostname(),
        "IP Address": socket.gethostbyname(socket.gethostname()),
        "Architecture": platform.machine(),
        "Processor": platform.processor(),
        "System Version": platform.version(),
        "Python Version": platform.python_version()
    }
    return system_info

def scan_open_ports():
    """扫描开放端口"""
    open_ports = []
    try:
        # 使用netstat命令（适用于Linux和Windows）
        output = subprocess.check_output(["netstat", "-an"], shell=True).decode()
        for line in output.splitlines():
            if "LISTEN" in line:
                parts = line.split()
                if ":" in parts[1]:
                    port = parts[1].split(":")[-1]
                    open_ports.append(port)
    except Exception as e:
        print(f"Error: {e}")
    return open_ports

def scan_network():
    """扫描网络信息"""
    network_info = {}
    try:
        with IPRoute() as ipr:
            routing_table = ipr.get_routes()
            network_info["Routing Table"] = routing_table
    except Exception as e:
        print(f"Error: {e}")
    return network_info

def get_system_users():
    """获取系统用户信息"""
    users = []
    for user in psutil.users():
        users.append({
            "Username": user.name,
            "Terminal": user.terminal,
            "Host": user.host,
            "Started": user.started
        })
    return users

def main():
    print("System Information:")
    system_info = get_system_info()
    for key, value in system_info.items():
        print(f"- {key}: {value}")
    
    print("\nScanning open ports...")
    open_ports = scan_open_ports()
    if open_ports:
        print("Open ports:")
        for port in open_ports:
            print(f"- {port}")
    else:
        print("No open ports found.")
    
    print("\nScanning network...")
    network_info = scan_network()
    for key, value in network_info.items():
        print(f"\n{key}:")
        print(value)
    
    print("\nGetting system users...")
    system_users = get_system_users()
    print("System Users:")
    for user in system_users:
        print(user)

if __name__ == "__main__":
    main()

# pip install psutil pyroute2
# sansjtw